package com.inspector.app.ui.task;

import static com.inspector.app.ui.vo.TaskClickType.APPLY_TYPE;
import static com.inspector.app.ui.vo.TaskClickType.PASSWORD_TYPE;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.ViewModelProvider;

import com.example.commen.net.easyHttp.model.HttpData;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.hjq.http.listener.HttpCallback;
import com.hjq.toast.Toaster;
import com.inspector.app.R;
import com.inspector.app.databinding.DialogTaskExecutionBinding;
import com.inspector.app.ui.vo.TaskClickType;
import com.inspector.app.ui.vo.TaskItem;


public class TaskExecutionDialog extends BottomSheetDialogFragment {

    private static TaskExecutionDialog instance;

    private TaskExecutionDialogModel taskExecutionDialogModel;
    private TaskClickType mClickType;

    private TaskItem mTaskItem;

    private TaskExecutionListener mTaskExecutionListener;

    public TaskExecutionDialog() {

    }

    public TaskExecutionDialog newInstance() {
        if (instance == null) {
            instance = new TaskExecutionDialog();
        }
        return instance;
    }


    public TaskExecutionDialog setType(TaskClickType clickType) {
        mClickType = clickType;
        return instance;
    }

    public TaskExecutionDialog setData(TaskItem taskItem) {
        mTaskItem = taskItem;
        return instance;
    }

    public TaskExecutionDialog setOnTaskExecutionListener(TaskExecutionListener taskExecutionListener) {
        mTaskExecutionListener = taskExecutionListener;
        return instance;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        if (dialog instanceof BottomSheetDialog) {
            BottomSheetDialog bottomSheetDialog = (BottomSheetDialog) dialog;

            bottomSheetDialog.setOnShowListener(dialogInterface -> {
                FrameLayout bottomSheet = bottomSheetDialog.findViewById(com.google.android.material.R.id.design_bottom_sheet);

                BottomSheetBehavior from = BottomSheetBehavior.from(bottomSheet);
                // 获取屏幕高度
                DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
                int screenHeight = displayMetrics.heightPixels;

                // 设置 peekHeight 为屏幕高度的三分之二
                int peekHeight = screenHeight * 3 / 5;
                ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) bottomSheet.getLayoutParams();
                layoutParams.height = peekHeight;
                bottomSheet.setLayoutParams(layoutParams);
                from.setPeekHeight(peekHeight);
                from.setDraggable(false);
                if (bottomSheet != null) {
                    bottomSheet.setBackgroundResource(R.drawable.shape_common_gray_top_10dp);
                }

            });
        }


        return dialog;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        DialogTaskExecutionBinding inflate = DialogTaskExecutionBinding.inflate(getLayoutInflater());
        inflate.setLifecycleOwner(this);
        taskExecutionDialogModel = new ViewModelProvider(this).get(TaskExecutionDialogModel.class);

        inflate.contentLayout.setVisibility(mClickType == PASSWORD_TYPE ? View.GONE : View.VISIBLE);
        inflate.passwordLayout.setVisibility(mClickType == PASSWORD_TYPE ? View.VISIBLE : View.GONE);
        inflate.title.setText(mClickType == PASSWORD_TYPE ? "密码查看" : "申请执行");


        inflate.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (mClickType) {
                    case PASSWORD_TYPE:
                        String password = inflate.edtPassword.getText().toString();
                        if (TextUtils.isEmpty(password)) {
                            Toaster.show("请输入密码");
                        } else {
                            taskExecutionDialogModel.getPassword(getViewLifecycleOwner(), new PasswordData(mTaskItem.getId(), password), new HttpCallback<HttpData<String>>() {
                                @Override
                                public void onSucceed(HttpData<String> result) {
                                    super.onSucceed(result);
                                    if (200 == result.getCode()) {
                                        inflate.edtPassword.setText("");
                                        inflate.edtReason.setText("");
                                        if (mTaskExecutionListener != null) {
                                            mTaskExecutionListener.onTaskExecutionSucceed(PASSWORD_TYPE);
                                        }
                                    } else {
                                        Toaster.show(result.getMessage());
                                    }
                                }


                                @Override
                                public void onFail(Exception e) {
                                    super.onFail(e);
                                    if (mTaskExecutionListener != null) {
                                        mTaskExecutionListener.error();
                                    }
                                }


                            });
                        }
                        break;
                    case APPLY_TYPE:
                        String reason = inflate.edtReason.getText().toString();
                        if (TextUtils.isEmpty(reason)) {
                            Toaster.show("请输入理由");
                        } else {
                            taskExecutionDialogModel.getReason(getViewLifecycleOwner(), new ReasonData(mTaskItem.getId(), reason), new HttpCallback<HttpData<String>>() {
                                @Override
                                public void onSucceed(HttpData<String> result) {
                                    super.onSucceed(result);
                                    if (200 == result.getCode()) {
                                        inflate.edtPassword.setText("");
                                        inflate.edtReason.setText("");
                                        if (mTaskExecutionListener != null) {
                                            mTaskExecutionListener.onTaskExecutionSucceed(APPLY_TYPE);
                                        }
                                    } else {
                                        Toaster.show(result.getMessage());
                                    }
                                }


                                @Override
                                public void onFail(Exception e) {
                                    super.onFail(e);
                                    if (mTaskExecutionListener != null) {
                                        mTaskExecutionListener.error();
                                    }
                                }


                            });

                        }

                        break;
                }

            }
        });

        inflate.closeImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        return inflate.getRoot();
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


    }


    public void showDialog(FragmentManager manager) {
        if (manager.findFragmentByTag(TaskExecutionDialog.class.getSimpleName()) == null) {
            instance.show(manager, TaskExecutionDialog.class.getSimpleName());
        }
    }


    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        super.onDismiss(dialog);

    }


    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    public interface TaskExecutionListener {
        void onTaskExecutionSucceed(TaskClickType mClickType);

        void error();
    }
}
